package com.moon.interface_api.controller;

import com.moon.interface_api.domain.UserDO;
import com.moon.interface_api.enums.ResultEnum;
import com.moon.interface_api.response.Result;
import com.moon.interface_api.service.UserService;
import com.moon.interface_api.utils.Query;
import com.moon.interface_api.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chengliang
 * @email 
 * @date 2018-11-06 13:15:36
 */
 
@Controller
@RequestMapping("/cms/user")
public class UserController {
	@Autowired
	private UserService userService;

	
	@ResponseBody
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
	public Result list(@RequestBody Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserDO> userList = userService.list(query);
		int total = userService.count(query);
		return Result.success(userList,null,null,total);
	}
	
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result save(@RequestBody UserDO user){
		user.setCreateTime(new Date());
		if(userService.save(user)>0){
			return Result.success();
		}
		return Result.error(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result update(@RequestBody UserDO user){
	    user.setUpdateTime(new Date());
		userService.update(user);
		return Result.success();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove( @RequestBody Map<String, Object> params){
		if(userService.remove(params)>0){
		return Result.success();
		}
		return Result.error(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result remove(@RequestParam("ids[]") Integer[] ids){
		userService.batchRemove(ids);
		return Result.success();
	}

    /**
     * 逻辑删除
     */
    @PostMapping( "/delete")
    @ResponseBody
    public Result delete(@RequestBody Map<String, Object> params){
        if(userService.delete(params)>0){
            return Result.success();
        }
        return Result.error(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
    }

    /**
     * 逻辑删除
     */
    @PostMapping( "/batchDelete")
    @ResponseBody
    public Result delete(@RequestParam("ids[]") Integer[] ids){
        if(userService.batchDelete(ids)>0){
            return Result.success();
        }
        return Result.error(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
    }
    
    /**
     * 判断是否填写资料
     */
    @RequestMapping( "/isWrite")
    @ResponseBody
    public Result isWrite(HttpServletRequest request){
		String token = request.getHeader("token");
		String userInfo = TokenUtils.getUserInfo(token);
		String phoneNum = TokenUtils.getUserInfo(token).split(",")[0];
		UserDO user = userService.getUserByPhoneNum(phoneNum);
		if(StringUtils.isEmpty(user.getCity()) 
				|| StringUtils.isEmpty(user.getUserName())
				|| StringUtils.isEmpty(user.getCardNum())
				|| StringUtils.isEmpty(user.getCompany())
				|| StringUtils.isEmpty(user.getCompanyPhoneNum())){
			return Result.error(ResultEnum.USER_DATE_NONE.getCode(),ResultEnum.USER_DATE_NONE.getMsg());
			
		}
		return Result.success();
    }
    
    
    
}
